/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GenerateTextDialog.java
 *
 * Created on Nov 12, 2010, 11:58:39 AM
 */
package se.jocke.tg.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JComponent;
import javax.swing.JEditorPane;
import javax.swing.KeyStroke;
import javax.swing.text.Document;
import javax.swing.text.EditorKit;
import org.netbeans.api.editor.DialogBinding;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.text.CloneableEditorSupport;
import org.openide.util.Exceptions;
import se.jocke.tg.TextGenerator;

/**
 *
 * @author jocke
 */
public class GenerateTextDialog extends javax.swing.JDialog {
    
    private static final GenerateTextDialog DIALOG = new GenerateTextDialog();
    private TextGenerator generator;

    /** Creates new form GenerateTextDialog */
    public GenerateTextDialog() {
        super();
        initComponents();
        
        try {
            
            EditorKit kit = CloneableEditorSupport.getEditorKit("text/x-html");
            
            FileObject fob = FileUtil.createMemoryFileSystem().getRoot().createData("tmp", "html");
            DataObject dob = DataObject.find(fob);
            editorPane.getDocument().putProperty(Document.StreamDescriptionProperty, dob);
            DialogBinding.bindComponentToFile(fob, 0, 0, editorPane);
            editorPane.setEditorKit(kit);
            
            KeyStroke keyStroke = KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.Event.CTRL_MASK);
            
            ActionListener delegate = new ActionListener() {
                
                @Override
                public void actionPerformed(ActionEvent e) {
                    generateButtonActionPerformed(e);
                }
            };
            
            editorPane.registerKeyboardAction(delegate, keyStroke, JComponent.WHEN_FOCUSED);
            
        } catch (DataObjectNotFoundException ex) {
            Exceptions.printStackTrace(ex);
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
        
    }
    
    public JEditorPane getEditorPane() {
        return editorPane;
    }
    
    public static GenerateTextDialog getDefault() {
        return DIALOG;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        editorPane = new javax.swing.JEditorPane();
        generateButton = new javax.swing.JButton();
        stringBuilderCheckBox = new javax.swing.JCheckBox();
        commentCheckBox = new javax.swing.JCheckBox();
        defaultCheckBox = new javax.swing.JCheckBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jScrollPane1.setViewportView(editorPane);

        generateButton.setText(org.openide.util.NbBundle.getMessage(GenerateTextDialog.class, "GenerateTextDialog.generateButton.text")); // NOI18N
        generateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateButtonActionPerformed(evt);
            }
        });

        stringBuilderCheckBox.setText(org.openide.util.NbBundle.getMessage(GenerateTextDialog.class, "GenerateTextDialog.stringBuilderCheckBox.text")); // NOI18N
        stringBuilderCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stringBuilderCheckBoxActionPerformed(evt);
            }
        });

        commentCheckBox.setText(org.openide.util.NbBundle.getMessage(GenerateTextDialog.class, "GenerateTextDialog.commentCheckBox.text")); // NOI18N
        commentCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                commentCheckBoxActionPerformed(evt);
            }
        });

        defaultCheckBox.setText(org.openide.util.NbBundle.getMessage(GenerateTextDialog.class, "GenerateTextDialog.defaultCheckBox.text")); // NOI18N
        defaultCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                defaultCheckBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(stringBuilderCheckBox)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(commentCheckBox)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(defaultCheckBox)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 451, Short.MAX_VALUE)
                        .addComponent(generateButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(generateButton)
                    .addComponent(stringBuilderCheckBox)
                    .addComponent(commentCheckBox)
                    .addComponent(defaultCheckBox))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void generateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateButtonActionPerformed
        if (generator != null) {
            if (stringBuilderCheckBox.isSelected()) {
                generator.generateText(editorPane.getText(), TextGenerator.FORMAT.STRING_BUILDER);
            } else if (commentCheckBox.isSelected()) {
                generator.generateText(editorPane.getText(), TextGenerator.FORMAT.COMMENT);
            } else {
                generator.generateText(editorPane.getText(), TextGenerator.FORMAT.DEFAULT);
            }
            editorPane.setText("");
            setVisible(false);
        }
    }//GEN-LAST:event_generateButtonActionPerformed
    
private void stringBuilderCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stringBuilderCheckBoxActionPerformed
    commentCheckBox.setSelected(Boolean.FALSE);
    defaultCheckBox.setSelected(Boolean.FALSE);
}//GEN-LAST:event_stringBuilderCheckBoxActionPerformed

private void commentCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_commentCheckBoxActionPerformed
    stringBuilderCheckBox.setSelected(Boolean.FALSE);
    defaultCheckBox.setSelected(Boolean.FALSE);
}//GEN-LAST:event_commentCheckBoxActionPerformed

private void defaultCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_defaultCheckBoxActionPerformed
  stringBuilderCheckBox.setSelected(Boolean.FALSE);
  commentCheckBox.setSelected(Boolean.FALSE);
}//GEN-LAST:event_defaultCheckBoxActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox commentCheckBox;
    private javax.swing.JCheckBox defaultCheckBox;
    private javax.swing.JEditorPane editorPane;
    private javax.swing.JButton generateButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JCheckBox stringBuilderCheckBox;
    // End of variables declaration//GEN-END:variables

    public void setTextGenerator(TextGenerator aGenerator) {
        this.generator = aGenerator;
    }
}
